package com.boot.main.service.impl;

import com.boot.main.utils.IsNotEmptyUtil;
import com.boot.main.entity.CcomMenu;
import com.boot.main.entity.CcomPermission;
import com.boot.main.mapper.CcomMenuMapper;
import com.boot.main.mapper.CcomPermissionMapper;
import com.boot.main.mapper.CcomRoleMenuMapper;
import com.boot.main.mapper.CcomRolePermissionMapper;
import com.boot.main.response.ResponseData;
import com.boot.main.response.ResponseDataUtil;
import com.boot.main.service.IMenuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Slf4j
@Service
public class MenuService implements IMenuService {
    @Resource
    private CcomMenuMapper menuMapper;

    @Resource
    private CcomRoleMenuMapper roleMenuMapper;

    @Resource
    private CcomRolePermissionMapper rolePermissionMapper;

    @Resource
    private CcomPermissionMapper permissionMapper;

    @Override
    public ResponseData getMenus(){
        List<CcomMenu> items = menuMapper.getMenus();
        if(!items.isEmpty()){
            for (CcomMenu item:items){

            }
            return ResponseDataUtil.success( "查询成功", items);
        }
        return ResponseDataUtil.success("暂无菜单信息！", items);
    }

    @Override
    public ResponseData saveMenu(CcomMenu item){
        int item_num = menuMapper.saveMenu(item);
        long item_id = item.getId();
        if(!IsNotEmptyUtil.isEmpty(item_num)) {
            return ResponseDataUtil.success( "新增菜单成功！", null);
        } else {
            return ResponseDataUtil.error("新增菜单失败！", null);
        }
    }

    @Override
    public ResponseData updateMenu(CcomMenu item){
        int item_num = menuMapper.updateMenu(item);
        if(!IsNotEmptyUtil.isEmpty(item_num)) {
            return ResponseDataUtil.success("修改菜单成功！", null);
        } else {
            return ResponseDataUtil.error( "修改菜单失败！", null);
        }
    }

    @Override
    public ResponseData deleteMenu(Long id){
        int item_num = menuMapper.deleteMenu(id);
        if(!IsNotEmptyUtil.isEmpty(item_num)) {
            return ResponseDataUtil.success("删除菜单成功！", null);
        } else {
            return ResponseDataUtil.error( "删除菜单失败！", null);
        }
    }

    @Override
    public ResponseData getMenuTree(){
        List<CcomMenu> items = menuMapper.getMenus();
        for(int i=0; i<items.size(); i++){
            List<CcomMenu> children = new ArrayList<>();
            for (int j=0; j<items.size(); j++){
                if(items.get(i).getId() == items.get(j).getPid()){
                    items.get(i).setChildren(children);
                    items.get(i).getChildren().add(items.get(j));
                }
            }
        }
        List<CcomMenu> arr = new ArrayList<>();
        for(int i=0; i<items.size(); i++){
            if (items.get(i).getPid() == 0){
                arr.add(items.get(i));
            }
        }
        return ResponseDataUtil.success("获取菜单树成功！", arr);
    }

    @Override
    public ResponseData getMenuTreeList(Long role_id){
        List<CcomMenu> items = menuMapper.getMenus();
        List itemArr = new ArrayList();
        ArrayList mIds = new ArrayList();
        String menu_ids = roleMenuMapper.getMenuByRoleId(role_id);
        if(menu_ids != null) {
            String[] menuIds = menu_ids.split(",");
            for(String menu_id:menuIds){
               mIds.add(menu_id);
            }
        }
        for(CcomMenu item:items){
            Map map = new HashMap();
            map.put("id",item.getId());
            map.put("pid", item.getPid());
            map.put("title", item.getTitle());
            if(mIds.size() > 0) {
                Integer index = mIds.indexOf(item.getId().toString());
                if (index >= 0) {
                    map.put("checked", true);
                } else {
                    map.put("checked", false);
                }
            } else {
                map.put("checked", false);
            }
            itemArr.add(map);
        }
        return ResponseDataUtil.success("获取菜单树成功！", itemArr);
    }

    @Override
    public ResponseData getMenuTreePermissions(Long role_id){
        List<CcomMenu> items = menuMapper.getMenus();
        List itemArr = new ArrayList();
        for(CcomMenu item:items){
            Map map = new HashMap();
            map.put("id",item.getId());
            map.put("pid", item.getPid());
            map.put("title", item.getTitle());
            List<CcomPermission> permissionItems = permissionMapper.getPermissionsByMenuId(item.getId());
            List permissions = new ArrayList();
            for(CcomPermission pItem:permissionItems){
                Map pMap = new HashMap();
                pMap.put("id",pItem.getId());
                pMap.put("name", pItem.getName());
                pMap.put("code", pItem.getCode());
                String mIds = rolePermissionMapper.getPermissionByRoleId(role_id);
                if(mIds != null) {
                    Integer index = mIds.indexOf(pItem.getId().toString());
                    if (index >= 0) {
                        pMap.put("checked", true);
                    } else {
                        pMap.put("checked", false);
                    }
                } else {
                    pMap.put("checked", false);
                }
                permissions.add(pMap);
            }
            map.put("permissions", permissions);
            itemArr.add(map);
        }
        return ResponseDataUtil.success("获取菜单树成功！", itemArr);
    }

    @Override
    public ResponseData getMenuPermissionsByUserRole(String role_ids){
        //List<String> list = Arrays.asList(arr);
        //String[] arr = list.toArray(new String[list.size()]);
        String[] roleIds = role_ids.split(",");
        List<String> menuIds = new ArrayList();
        String[] pIds = rolePermissionMapper.getPermissionByRoleIds(roleIds);
        String permissionIds = String.join(",", pIds);
        for(String roleId:roleIds){
            System.out.println(Long.parseLong(roleId));
            String menuId = roleMenuMapper.getMenuByRoleId(Long.parseLong(roleId));
            if(IsNotEmptyUtil.isNotEmpty(menuId)) {
                String[] roleMenuIds = menuId.split(",");
                for (String roleMenu : roleMenuIds) {
                    if (menuIds.indexOf(roleMenu) < 0) {
                        menuIds.add(roleMenu);
                    }
                }
            }
        }
        List<CcomMenu> items = menuMapper.getMenuByIds(menuIds);
        for(CcomMenu item:items){
            List pArr = new ArrayList();
            List<CcomPermission> permissions = permissionMapper.getPermissionsByMenuId(item.getId());
            for(CcomPermission permission:permissions){
                if(permissionIds.indexOf(permission.getId().toString()) >= 0){
                    pArr.add(permission.getCode());
                }
            }
            item.setPermission(pArr);
        }
        List<CcomMenu> menuList= getRoot(items);
        for(CcomMenu menuItem:menuList){
            generateTree(items, menuItem);
        }
        return ResponseDataUtil.success("获取用户菜单权限成功", menuList);
    }

    public static List<CcomMenu> getRoot(List<CcomMenu> items){
        List<CcomMenu> menuList = new ArrayList<CcomMenu>();
        for(CcomMenu item:items){
            if(item.getPid() == 0){
                menuList.add(item);
            }
        }
        return menuList;
    }
    public static CcomMenu generateTree(List<CcomMenu> items, CcomMenu menu){
        List<CcomMenu> children = new ArrayList<>();
        for(CcomMenu item:items){
            if(item.getPid()== menu.getId()){
                if(IsNotEmptyUtil.isEmpty(menu.getChildren())){
                    menu.setChildren(children);
                    menu.getChildren().add(item);
                }else {
                    menu.getChildren().add(item);
                }
                generateTree(items, item);
            }
        }
        return menu;
    }
}
